/**
 * 测试施工队伍状态和权限
 * 排查"您还没有注册施工队伍或尚未通过审核"的问题
 */

const testConstructionTeamStatus = () => {
  console.log('🔍 开始检查施工队伍状态问题...')
  
  console.log('\n📋 可能的问题原因:')
  console.log('1. 状态字段值不匹配 (status字段)')
  console.log('2. openid字段不匹配')
  console.log('3. canReceiveOrders字段缺失或为false')
  console.log('4. 数据库集合名称不正确')
  console.log('5. 权限验证逻辑有误')
  
  console.log('\n🔧 需要检查的字段:')
  console.log('constructionTeams集合中的字段:')
  console.log('- openid: 用户的微信openid')
  console.log('- status: 审核状态 (可能是 "approved", "passed", "通过" 等)')
  console.log('- canReceiveOrders: 是否可以接单')
  console.log('- name: 施工队伍名称')
  console.log('- _id: 唯一标识')
  
  console.log('\n📝 当前云函数中的查询条件:')
  console.log('```javascript')
  console.log('db.collection("constructionTeams")')
  console.log('  .where({')
  console.log('    openid: openid,')
  console.log('    status: "approved"  // <-- 这里可能有问题')
  console.log('  })')
  console.log('```')
  
  console.log('\n🎯 可能的status字段值:')
  console.log('- "approved" (英文)')
  console.log('- "passed" (英文)')
  console.log('- "通过" (中文)')
  console.log('- "审核通过"')
  console.log('- "active"')
  console.log('- 1 (数字)')
  console.log('- true (布尔值)')
  
  console.log('\n🔍 调试建议:')
  console.log('1. 先查询当前用户的constructionTeams记录')
  console.log('2. 确认status字段的实际值')
  console.log('3. 确认openid是否匹配')
  console.log('4. 修改云函数中的查询条件')
  
  console.log('\n💡 临时解决方案 - 修改查询条件:')
  console.log('```javascript')
  console.log('// 方案1: 移除status条件，只查询openid')
  console.log('const teamResult = await db.collection("constructionTeams")')
  console.log('  .where({ openid: openid })')
  console.log('  .get()')
  console.log('')
  console.log('// 方案2: 使用多个可能的status值')
  console.log('const teamResult = await db.collection("constructionTeams")')
  console.log('  .where({')
  console.log('    openid: openid,')
  console.log('    status: _.in(["approved", "passed", "通过", "审核通过"])')
  console.log('  })')
  console.log('  .get()')
  console.log('```')
}

const showFixSteps = () => {
  console.log('\n🚀 修复步骤:')
  console.log('1. 查询数据库确认status字段的实际值')
  console.log('2. 修改teamOrderManager云函数的getTeamInfo方法')
  console.log('3. 重新部署云函数')
  console.log('4. 测试功能是否恢复正常')
  
  console.log('\n⚡ 快速修复建议:')
  console.log('修改getTeamInfo函数中的查询条件:')
  console.log('从: status: "approved"')
  console.log('改为: 不限制status或使用正确的status值')
}

// 运行测试
testConstructionTeamStatus()
showFixSteps()

module.exports = { testConstructionTeamStatus } 